package com.ctesi.service.zhaobiao.guangdong;

import com.ctesi.utils.PutUtil;
import com.ctesi.utils.StringExtract;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.time.LocalDateTime;
import java.util.Random;
//https://ygp.gdzwfw.gov.cn/#/44/jygg - post请求
@Service
public class GuangDongProcessorService implements PageProcessor {
    private String bidType;
    private Site site;
    @Override
    public void process(Page page) {
        Random random = new Random();
        // 生成5000到10000之间的随机整数
        int randomTime = random.nextInt(10000 - 5000 + 1) + 5000;
        //设置请求头和等待时间
        this.site = Site.me()
                .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36")
                .setTimeOut(randomTime) // 设置超时时间为5秒到10秒之间的数
                .setRetryTimes(5); // 设置重试次数为 3 次
        site.addHeader("Connection", "keep-alive");

        String currentUrl = page.getUrl().get();
        if (!currentUrl.contains("projectCode")){
            //裂变url执行逻辑

        }else {
            //爬取内容逻辑
            System.out.println("----------开启爬取内容--------------");

            //招标主体信息
            String bidContext = page.getHtml().xpath("/html/body/div[12]/div[2]/div/div[3]").get();
            bidContext = StringExtract.removeEmptyLines(StringExtract.removeNbsp(StringExtract.removeTags(bidContext)));
            System.out.println(bidContext);
            //招标编号
            PutUtil.putField(page,bidContext,"招标项目编号：(.*?)\\s","bidId");

            //招标类型
            page.putField("bidType",bidType);

            //招标省份
            page.putField("bidProvince","广西省");

            //招标范围
            PutUtil.putField(page,bidContext,"招标范围：(.*?)建设规模","bidArange");

            //招标发布时间
            String bidPubTime = page.getHtml().xpath("/html/body/div[12]/div[2]/div/div[2]/text()").toString();
            System.out.println(bidPubTime);
            PutUtil.putField(page,bidPubTime,"(.*?)-(.*?)-(.*?)}","bidPubTime");

            //备注：没有该字段

            //数据创建时间
            LocalDateTime createTime = LocalDateTime.now();
            page.putField("createTime",createTime);

            //数据更新时间
            LocalDateTime updateTime = LocalDateTime.now();
            page.putField("updateTime",updateTime);

            //公告url
            page.putField("bidUrl",page.getUrl().get());

            //招标地区
            PutUtil.putField(page,bidContext,"建设地点：(.*?)\\n","bidArea");

            //投标人资格要求
            PutUtil.putField(page,bidContext,"投标人资格要求：(.*?)\\n","bidCredit");

            //评标办法
            PutUtil.putField(page,bidContext,"办法采用：(.*?)\\n","bidMethod");


            //招标状态：没有这个字段

            //招标人信息

            //招标机构
            PutUtil.putField(page,bidContext,"招标人：(.*?)\\n","bidUnity");

            //招标联系人
            PutUtil.putField(page,bidContext,"招标人：.*?\\n(.*?)\\n","bidName");

            //招标联系人电话
            PutUtil.putField(page,bidContext,"招标人：.*?\\n.*?电话:(.*?)\\n","bidPhone");

            //招标地址
            PutUtil.putField(page,bidContext,"招标人:.*?地址(.*?)\\n","bidAddress");

            //代理机构信息

            //代理机构
            PutUtil.putField(page,bidContext,"招标代理机构:(.*?)\\n","agencyUnity");

            //代理机构联系人
            PutUtil.putField(page,bidContext,"招标代理机构:.*?联系人:(.*?)\\n","agencyName");

            //代理机构联系人电话
            PutUtil.putField(page,bidContext,"招标代理机构:.*?电话:(.*?)\\n","agencyPhone");

            //代理机构地址
            PutUtil.putField(page,bidContext,"招标代理机构:.*?地址:(.*?)\\n","agencyAddress");
        }
    }

    @Override
    public Site getSite() {
        return PageProcessor.super.getSite();
    }
}
